from flask import Flask, jsonify
from pyModbusTCP.client import ModbusClient

app = Flask(__name__)

@app.route('/six_arm_status')
def get_six_arm_status():
    """
    获取机械臂状态

    返回:
        {'status': 'take'}: 取料状态
        {'status': 'put'}: 放料状态
        {'status': 'notwork'}: 机械臂未工作状态
        {'error': 'connect failed'}: 连接失败
    """
    # 设置Modbus TCP客户端
    c = ModbusClient(host="192.168.1.30", port=502)  # 替换成你的设备IP地址和端口号

    if not c.open():
        return jsonify({'error': 'connect failed'})

    addr = 0x0001
    arm_status = c.read_holding_registers(addr, 1)[0]  # 读取对应寄存器值

    c.close()  # 关闭连接

    if arm_status == 1:
        return jsonify({'status': 'take'})
    elif arm_status == 3:
        return jsonify({'status': 'put'})
    else:
        return jsonify({'status': 'notwork'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

